﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

using BLL;
using DTO;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmKPIList : DevExpress.XtraEditors.XtraForm
    {
        public frmKPIList()
        {
            InitializeComponent();
            grdViewKPI.BestFitColumns();
            grdViewKPI.OptionsView.ColumnAutoWidth = false;
            grdViewKPI.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewKPI.HorzScrollVisibility = ScrollVisibility.Always;
            cmbCollector.DataSource = SharedData.CollectorList;
            cmbCollector.DisplayMember = "Collector_Code";
            cmbCollector.ValueMember = "Collector_ID";
            cmbCurrentBrand.DataSource = SharedData.BrandList;
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";
           
            cmdPreviousBrand.DataSource = SharedData.BrandList;
            cmdPreviousBrand.DisplayMember ="Brand_Code" ;
            cmdPreviousBrand.ValueMember = "Brand_ID";
        }

        DataTranferEntity trans = new DataTranferEntity();
        BLsupervisor sup = new BLsupervisor();
        private List<CustomerEntity> _KPIList1 = new List<CustomerEntity>();
        private List<CustomerEntity> _KPIList2 = new List<CustomerEntity>();

        #region IntialInterface
        private void mItemSource_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemSource.CheckState == CheckState.Checked)
            {
                grdBaSource.Visible = true;
            }
            else
            {
                grdBaSource.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemMother_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemMother.CheckState == CheckState.Checked)
            {
                grdBaMother.Visible = true;
            }
            else
            {
                grdBaMother.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemAddress_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemAddress.CheckState == CheckState.Checked)
            {
                grdBaAdress.Visible = true;
            }
            else
            {
                grdBaAdress.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemEDD_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemEDD.CheckState == CheckState.Checked)
            {
                grdBaEDD.Visible = true;
            }
            else
            {
                grdBaEDD.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemChild_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemChild.CheckState == CheckState.Checked)
            {
                grdBaChild.Visible = true;
            }
            else
            {
                grdBaChild.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mbtnDetail_Click(object sender, EventArgs e)
        {
            mbtnDetail.ShowDropDown();
        }
        #endregion


        private void cmbSource_Click(object sender, EventArgs e)
        {
            cmbSource.Items.Clear();
            var query = sup.loadSourceKPI(dtpStartDate.Value, dtpEndDate.Value);
            foreach (SourceEntity sc in query)
            {
                cmbSource.Items.Add(sc.SourceName);
            }            
        }

        private void cmbSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnKPI.Enabled = true;
        }

        private void mbtnKPI_Click(object sender, EventArgs e)
        {
            try
            {
                LoadCustomerFunc func = new LoadCustomerFunc();
                DataTranferEntity trans = new DataTranferEntity();
                string SourceName = cmbSource.SelectedItem.ToString();
                int SourceID = sup.loadSourceKPI(dtpStartDate.Value, dtpEndDate.Value).Where(a => a.SourceName == SourceName).SingleOrDefault().Source_ID;
                Regex Reg = new Regex(@"^\d+$");
                if (SourceName != "")
                {
                    int CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NON").SingleOrDefault().Status_ID;
                    int CallStatus = SharedData.CallStatusList.Where(a => a.CallStatusName == "KPI").SingleOrDefault().CallStatus_ID;
                    var query = (from c in func.LoadCustomerByAll()
                                 where (c.Source_ID == SourceID) && ((c.CustomerStatus == CustomerStatus)  && (c.CallStatus ==  CallStatus))
                                 select c).ToList();

                    if (query.Count == 0 && query != null)
                    {
                        grdKPI.DataSource = null;
                        MessageBox.Show("Không có dữ liệu để chia KPI ở nguồn đã chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        cmbSource.Focus();
                    }
                    else
                    {
                        if (txtKPIPercent.TextLength <= 0)
                        {
                            MessageBox.Show("Chưa nhập tỉ số KPI", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtKPIPercent.Focus();
                        }
                        else if (Reg.IsMatch(txtKPIPercent.Text) != true)
                        {
                            MessageBox.Show("Tỉ số KPI không hợp lệ \n" + "(Tỉ số KPI chỉ có thể là số nằm trong khoảng 1~100)", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtKPIPercent.Focus();
                        }
                        else if (int.Parse(txtKPIPercent.Text) > 100)
                        {
                            MessageBox.Show("Tỉ số KPI không hợp lệ \n" + "(Tỉ số KPI chỉ có thể là số nằm trong khoảng 1~100)", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtKPIPercent.Focus();
                        }
                        else
                        {
                            int KPListCount = (Int16.Parse(txtKPIPercent.Text) * query.Count) /100;
                            _KPIList2 = Randomize(query);                          
                            _KPIList1 = _KPIList2.Take( KPListCount ).ToList() ;
                            _KPIList2 = _KPIList2.Skip(KPListCount).ToList();
                            grdKPI.DataSource = _KPIList1;
                            btnAcceptKPI.Enabled = true;
                            btnList2.Enabled = true;
                        }
                    }

                }
                else
                {

                    MessageBox.Show("Chưa chọn dữ liệu Source", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                return;
            }
        }

        private void btnAcceptKPI_Click(object sender, EventArgs e)
        {
            try
            {
                if (_KPIList1.Count == 0)
                {
                    MessageBox.Show("Chưa chọn dữ liệu để làm danh sách KPI", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    btnAcceptKPI.Enabled = false;
                    txtKPIPercent.Focus();
                }
                else
                {
                    DialogResult result = MessageBox.Show("Chọn danh sách này làm danh sách KPI???", "Xác nhận", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        sup.UpdateKPIList(_KPIList1);
                        btnAcceptKPI.Enabled = false;
                        MessageBox.Show("Làm danh sách KPI thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch
            {
                return;
            }
        }

        public static List<T> Randomize<T>(List<T> list)
        {
            List<T> randomizedList = new List<T>();
            Random rnd = new Random();
            while (list.Count > 0)
            {
                int index = rnd.Next(0, list.Count); //pick a random item from the master list
                randomizedList.Add(list[index]); //place it at the end of the randomized list
                list.RemoveAt(index);
            }
            return randomizedList;
        }

      

        private void btnList1_Click(object sender, EventArgs e)
        {
            btnList1.Enabled = false;
            btnList2.Enabled = true;
            grdKPI.DataSource = _KPIList1;

        }

        private void btnList2_Click_1(object sender, EventArgs e)
        {
            btnList2.Enabled = false;
            btnList1.Enabled = true;
            grdKPI.DataSource = _KPIList2;

        }

      
        
    }
}